﻿@{
  // Set the layout page and page title
  Layout = "~/_SiteLayout.cshtml";
  Page.Title = "Register for Reports Access";

  // Initialize general page variables
  var email = "";
  var reason = "";
  var other = "";

  // Validation
  var isValid = true;
  var accountCreationErrorMessage = "";
  
  if (!Request.ClientCertificate.IsPresent)
  {
    isValid = false;
    accountCreationErrorMessage = "CAC info not present. Unable to register. Please contact Admin in About page for further assistance.";
  }

  // If this is a POST request, validate and process data
  if (IsPost)
  {
    email = Request.Form["email"];

    // Validate the user's email address
    if (email.IsEmpty())
    {
      isValid = false;
      accountCreationErrorMessage += "<br/><br/>Please specify an email address.";
    }

    reason = Request.Form["reason"];
    other = Request.Form["other"];
    
    // Validate the user's email address
    if (reason.IsEmpty() || (reason == "Other" && other.IsEmpty()))
    {
      isValid = false;
      accountCreationErrorMessage += "<br/><br/>Please specify Reason For Access.";
    }

    // If all information is valid, create a new account
    if (isValid)
    {
      using (var accessRegister = new Proc("AccessRegister"))
      {
        accessRegister["@CACInfo"] = Request.ClientCertificate.Subject;
        accessRegister["@email"] = email;
        accessRegister["@reason"] = reason;
        accessRegister["@other"] = other;
        try
        {
          accessRegister.ExecuteNonQuery();
          accountCreationErrorMessage = "Your registration has been successfully submitted. You will receive a confirmation email once your access is approved.";
        }
        catch(Exception ex)
        {
          if (ex.Message.Contains("duplicate"))
          {
            accountCreationErrorMessage = "Your registration has already been submitted.";
          }
        }
      }
    }
  }
  
}

<p>
   Use the form below to request a new account. 
</p>

@* If at least one validation error exists, notify the user *@
  @if (!accountCreationErrorMessage.IsEmpty())
  {
    <p class="message error">
      @Html.Raw(accountCreationErrorMessage)
    </p>
  }

<form method="post" action="">
  <fieldset>
    <legend>Sign-up Form</legend>
    <ol>
      <li class="email">
        <label for="email" style="margin-bottom: 5px;">Email where you would like to be notified when your access is available:</label>
        <input type="text" id="email" name="email" title="Email address" value="@email" />
      </li>
      <li class="reason">
        <label for="reason" style="margin-bottom: 5px;">Reason For Access:</label>
        @Html.RadioButton("reason", "Customs") MP Customs<br/>
        @Html.RadioButton("reason", "VATOversight") VAT Office Oversight<br/>
        @Html.RadioButton("reason", "Other") Other: 
        <input type="text" id="other" name="other" title="Other reason" value="@other" />
      </li>
    </ol>
    <p class="form-actions">
      <input type="submit" value="Register" title="Register" />
    </p>
  </fieldset>
</form>